[id].vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <template>
  2. <div class="admin--popup-form">
  3. <div v-if="isLoading" class="admin--loading">
  4. 데이터를 불러오는 중...
  5. </div>
  6. <form v-else @submit.prevent="handleSubmit" class="admin--form">
  7. <!-- 형태 -->
  8. <div class="admin--form-group">
  9. <label class="admin--form-label">형태 <span class="admin--required">*</span></label>
  10. <div class="admin--radio-group">
  11. <label class="admin--radio-label">
  12. <input
  13. v-model="formData.type"
  14. type="radio"
  15. value="html"
  16. name="type"
  17. >
  18. <span>HTML</span>
  19. </label>
  20. <label class="admin--radio-label">
  21. <input
  22. v-model="formData.type"
  23. type="radio"
  24. value="image"
  25. name="type"
  26. >
  27. <span>단일페이지</span>
  28. </label>
  29. </div>
  30. </div>
  31. <!-- 제목 -->
  32. <div class="admin--form-group">
  33. <label class="admin--form-label">제목 <span class="admin--required">*</span></label>
  34. <input
  35. v-model="formData.title"
  36. type="text"
  37. class="admin--form-input"
  38. placeholder="팝업 제목을 입력하세요"
  39. required
  40. >
  41. </div>
  42. <!-- 시작일/종료일 -->
  43. <div class="admin--form-group">
  44. <label class="admin--form-label">시작일/종료일 <span class="admin--required">*</span></label>
  45. <div class="admin--date-range">
  46. <input
  47. v-model="formData.start_date"
  48. type="date"
  49. class="admin--form-input"
  50. required
  51. >
  52. <span class="admin--date-separator">~</span>
  53. <input
  54. v-model="formData.end_date"
  55. type="date"
  56. class="admin--form-input"
  57. required
  58. >
  59. </div>
  60. </div>
  61. <!-- 팝업창 사이즈 -->
  62. <div class="admin--form-group">
  63. <label class="admin--form-label">팝업창 사이즈 <span class="admin--required">*</span></label>
  64. <div class="admin--size-group">
  65. <div class="admin--size-item">
  66. <label>가로</label>
  67. <input
  68. v-model.number="formData.width"
  69. type="number"
  70. class="admin--form-input"
  71. placeholder="800"
  72. min="100"
  73. required
  74. >
  75. <span>px</span>
  76. </div>
  77. <div class="admin--size-item">
  78. <label>세로</label>
  79. <input
  80. v-model.number="formData.height"
  81. type="number"
  82. class="admin--form-input"
  83. placeholder="600"
  84. min="100"
  85. required
  86. >
  87. <span>px</span>
  88. </div>
  89. </div>
  90. </div>
  91. <!-- 팝업창 위치 -->
  92. <div class="admin--form-group">
  93. <label class="admin--form-label">팝업창 위치 <span class="admin--required">*</span></label>
  94. <div class="admin--size-group">
  95. <div class="admin--size-item">
  96. <label>TOP</label>
  97. <input
  98. v-model.number="formData.position_top"
  99. type="number"
  100. class="admin--form-input"
  101. placeholder="100"
  102. min="0"
  103. required
  104. >
  105. <span>px</span>
  106. </div>
  107. <div class="admin--size-item">
  108. <label>LEFT</label>
  109. <input
  110. v-model.number="formData.position_left"
  111. type="number"
  112. class="admin--form-input"
  113. placeholder="100"
  114. min="0"
  115. required
  116. >
  117. <span>px</span>
  118. </div>
  119. </div>
  120. </div>
  121. <!-- 쿠키설정 -->
  122. <div class="admin--form-group">
  123. <label class="admin--form-label">쿠키설정</label>
  124. <div class="admin--radio-group">
  125. <label class="admin--radio-label">
  126. <input
  127. v-model="formData.cookie_type"
  128. type="radio"
  129. value="today"
  130. name="cookie_type"
  131. >
  132. <span>오늘 하루 창 띄우지 않음</span>
  133. </label>
  134. <label class="admin--radio-label">
  135. <input
  136. v-model="formData.cookie_type"
  137. type="radio"
  138. value="forever"
  139. name="cookie_type"
  140. >
  141. <span>다시는 창을 띄우지 않음</span>
  142. </label>
  143. <label class="admin--radio-label">
  144. <input
  145. v-model="formData.cookie_type"
  146. type="radio"
  147. value="none"
  148. name="cookie_type"
  149. >
  150. <span>사용 안 함</span>
  151. </label>
  152. </div>
  153. </div>
  154. <!-- 출력내용 (HTML) -->
  155. <div v-if="formData.type === 'html'" class="admin--form-group">
  156. <label class="admin--form-label">출력내용 <span class="admin--required">*</span></label>
  157. <SunEditor
  158. v-model="formData.content"
  159. height="400px"
  160. placeholder="팝업 내용을 입력하세요"
  161. />
  162. </div>
  163. <!-- 출력내용 (이미지) -->
  164. <div v-if="formData.type === 'image'" class="admin--form-group">
  165. <label class="admin--form-label">이미지 첨부 <span class="admin--required">*</span></label>
  166. <input
  167. type="file"
  168. accept="image/*"
  169. class="admin--form-file"
  170. @change="handleImageUpload"
  171. >
  172. <div v-if="imagePreview || formData.image_url" class="admin--image-preview">
  173. <img :src="imagePreview || formData.image_url" alt="미리보기">
  174. <button
  175. type="button"
  176. class="admin--btn-remove-image"
  177. @click="removeImage"
  178. >
  179. 삭제
  180. </button>
  181. </div>
  182. </div>
  183. <!-- 링크 URL (단일페이지일 경우) -->
  184. <div v-if="formData.type === 'image'" class="admin--form-group">
  185. <label class="admin--form-label">링크 URL</label>
  186. <input
  187. v-model="formData.link_url"
  188. type="url"
  189. class="admin--form-input"
  190. placeholder="https://example.com"
  191. >
  192. </div>
  193. <!-- 버튼 영역 -->
  194. <div class="admin--form-actions">
  195. <button
  196. type="submit"
  197. class="admin--btn admin--btn-primary"
  198. :disabled="isSaving"
  199. >
  200. {{ isSaving ? '저장 중...' : '확인' }}
  201. </button>
  202. <button
  203. type="button"
  204. class="admin--btn admin--btn-secondary"
  205. @click="goToList"
  206. >
  207. 목록
  208. </button>
  209. </div>
  210. <!-- 성공/에러 메시지 -->
  211. <div v-if="successMessage" class="admin--alert admin--alert-success">
  212. {{ successMessage }}
  213. </div>
  214. <div v-if="errorMessage" class="admin--alert admin--alert-error">
  215. {{ errorMessage }}
  216. </div>
  217. </form>
  218. </div>
  219. </template>
  220. <script setup>
  221. import { ref, onMounted } from 'vue'
  222. import { useRoute, useRouter } from 'vue-router'
  223. import SunEditor from '~/components/admin/SunEditor.vue'
  224. definePageMeta({
  225. layout: 'admin',
  226. middleware: ['auth']
  227. })
  228. const route = useRoute()
  229. const router = useRouter()
  230. const { get, put, upload } = useApi()
  231. const isLoading = ref(true)
  232. const isSaving = ref(false)
  233. const successMessage = ref('')
  234. const errorMessage = ref('')
  235. const imagePreview = ref(null)
  236. const imageFile = ref(null)
  237. const formData = ref({
  238. type: 'html',
  239. title: '',
  240. start_date: '',
  241. end_date: '',
  242. width: 800,
  243. height: 600,
  244. position_top: 100,
  245. position_left: 100,
  246. cookie_type: 'none',
  247. content: '',
  248. image_url: '',
  249. link_url: ''
  250. })
  251. // 데이터 로드
  252. const loadPopup = async () => {
  253. isLoading.value = true
  254. const id = route.params.id
  255. const { data, error } = await get(`/basic/popup/${id}`)
  256. if (data) {
  257. formData.value = {
  258. type: data.type || 'html',
  259. title: data.title || '',
  260. start_date: data.start_date || '',
  261. end_date: data.end_date || '',
  262. width: data.width || 800,
  263. height: data.height || 600,
  264. position_top: data.position_top || 100,
  265. position_left: data.position_left || 100,
  266. cookie_type: data.cookie_type || 'none',
  267. content: data.content || '',
  268. image_url: data.image_url || '',
  269. link_url: data.link_url || ''
  270. }
  271. }
  272. isLoading.value = false
  273. }
  274. // 이미지 업로드
  275. const handleImageUpload = (event) => {
  276. const file = event.target.files[0]
  277. if (!file) return
  278. if (!file.type.startsWith('image/')) {
  279. alert('이미지 파일만 업로드 가능합니다.')
  280. return
  281. }
  282. imageFile.value = file
  283. // 미리보기
  284. const reader = new FileReader()
  285. reader.onload = (e) => {
  286. imagePreview.value = e.target.result
  287. }
  288. reader.readAsDataURL(file)
  289. }
  290. // 이미지 삭제
  291. const removeImage = () => {
  292. imagePreview.value = null
  293. imageFile.value = null
  294. formData.value.image_url = ''
  295. }
  296. // 폼 제출
  297. const handleSubmit = async () => {
  298. successMessage.value = ''
  299. errorMessage.value = ''
  300. // 유효성 검사
  301. if (!formData.value.title) {
  302. errorMessage.value = '제목을 입력하세요.'
  303. return
  304. }
  305. if (!formData.value.start_date || !formData.value.end_date) {
  306. errorMessage.value = '시작일과 종료일을 선택하세요.'
  307. return
  308. }
  309. if (formData.value.type === 'html' && !formData.value.content) {
  310. errorMessage.value = '출력내용을 입력하세요.'
  311. return
  312. }
  313. if (formData.value.type === 'image' && !imageFile.value && !formData.value.image_url) {
  314. errorMessage.value = '이미지를 첨부하세요.'
  315. return
  316. }
  317. isSaving.value = true
  318. try {
  319. let imageUrl = formData.value.image_url
  320. // 이미지 업로드 (새로운 이미지가 있는 경우)
  321. if (formData.value.type === 'image' && imageFile.value) {
  322. const formDataImage = new FormData()
  323. formDataImage.append('image', imageFile.value)
  324. const { data: uploadData, error: uploadError } = await upload('/upload/image', formDataImage)
  325. if (uploadError) {
  326. errorMessage.value = '이미지 업로드에 실패했습니다.'
  327. isSaving.value = false
  328. return
  329. }
  330. imageUrl = uploadData.url
  331. }
  332. // 팝업 수정
  333. const submitData = {
  334. ...formData.value,
  335. image_url: imageUrl
  336. }
  337. const id = route.params.id
  338. const { data, error } = await put(`/basic/popup/${id}`, submitData)
  339. if (error) {
  340. errorMessage.value = error.message || '수정에 실패했습니다.'
  341. } else {
  342. successMessage.value = '팝업이 수정되었습니다.'
  343. setTimeout(() => {
  344. router.push('/admin/basic/popup')
  345. }, 1000)
  346. }
  347. } catch (error) {
  348. errorMessage.value = '서버 오류가 발생했습니다.'
  349. console.error('Save error:', error)
  350. } finally {
  351. isSaving.value = false
  352. }
  353. }
  354. // 목록으로 이동
  355. const goToList = () => {
  356. router.push('/admin/basic/popup')
  357. }
  358. onMounted(() => {
  359. loadPopup()
  360. })
  361. </script>